package com.btl.ui.adapter;

import java.util.List;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
/**
 * 重写下面4个方法完成图片的轮播
 * 1 instantiateItem(ViewGroup, int)
 * 2. destroyItem(ViewGroup, int, Object)
 * 3. getCount()
 * 4. isViewFromObject(View, Object) 
 * @author zkx
 *
 */
public class ViewPaperAdapter extends PagerAdapter {

	 private List<View> views = null;

	    public ViewPaperAdapter(List<View> views) {
	        this.views = views;
	    }
	   // PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
	    @Override
	    public void destroyItem(View arg0, int arg1, Object arg2) {
	        ((ViewPager) arg0).removeView(views.get(arg1));
	    }

	    @Override
	    public void finishUpdate(View arg0) {

	    }

	    //获取要滑动图片的数量
	    @Override
	    public int getCount() {

	        return views.size();
	    }

	    //// 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView加入到ViewGroup中，然后作为返回值返回即可
	    @Override
	    public Object instantiateItem(View arg0, int arg1) {

	        ((ViewPager) arg0).addView(views.get(arg1), 0);
	        return views.get(arg1);
	    }
	    
	    //来判断显示的是否是同一张图片，这里我们将两个参数相比较返回即可
	    @Override
	    public boolean isViewFromObject(View arg0, Object arg1) {

	        return arg0 == arg1;
	    }

	    @Override
	    public void restoreState(Parcelable arg0, ClassLoader arg1) {

	    }

	    @Override
	    public Parcelable saveState() {

	        return null;
	    }

	    @Override
	    public void startUpdate(View arg0) {

	    }


}
